<?php

namespace App\Third;
use GuzzleHttp;
class Wechat
{
    /**
     * 微信小程序授权登录token
     */
    public static function getAppToken()
    {
        $param = [
            'appid' => config('wxpay.appAPPID'),
            'secret' => config('wxpay.appSecret'),
            'grant_type' => 'client_credential',
        ];
        $http = new GuzzleHttp\Client();
        $response = $http->request('GET', 'https://api.weixin.qq.com/cgi-bin/token?', [
            'query' => $param
        ]);
        $result = json_decode($response->getBody()->getContents(), true);
        return $result['access_token'];
    }

    /**
     * 微信公众号授权登录token
     */

    public static function getWxToken($code)
    {
        $param = [
            'appid' => config('wxpay.APPID'),
            'secret' => config('wxpay.Secret'),
            'code' => $code,
            'grant_type' => 'authorization_code',
        ];

        $http = new GuzzleHttp\Client();

        $response = $http->request('GET', 'https://api.weixin.qq.com/sns/oauth2/access_token?', [
            'query' => $param
        ]);
        $result =  json_decode($response->getBody()->getContents(), true);
        return $result['access_token'];
    }

    /**
     * 微信公众号AccessToken
     */

    public static function getWechatToken() {
        $param = [
            'grant_type' => 'client_credential',
            'appid' => config('wx.Appid'),
            'secret' => config('wx.Secret')
        ];
        $http = new GuzzleHttp\Client();
        $response = $http->request('GET', 'https://api.weixin.qq.com/cgi-bin/token?', [
            'query' => $param
        ]);
        $result = json_decode($response->getBody()->getContents(), true);
        return $result['access_token'];
    }
}
